package com.haierCamera.customapplication.api.repo;

import android.graphics.Bitmap;

import androidx.lifecycle.LiveData;

import com.haierCamera.customapplication.Constants;
import com.haierCamera.customapplication.api.ApiService;
import com.haierCamera.customapplication.api.vo.GetPushEntity;
import com.haierCamera.customapplication.api.vo.GetSystemRecord;
import com.haierCamera.customapplication.api.vo.GetUserAddress;
import com.haierCamera.customapplication.api.vo.GetUserInfoResponse;
import com.haierCamera.customapplication.common.Resource;
import com.haierCamera.customapplication.utils.ImageUtils;
import com.haierCamera.customapplication.utils.ResourceConvertUtils;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * @author yawei
 * @data on 2018/8/3 下午2:53
 * @email zyawei@live.com
 */
@Singleton
public class UserRepo {
    private ApiService apiService;

    @Inject
    public UserRepo(ApiService apiService) {
        this.apiService = apiService;
    }

    public LiveData<Resource<GetUserInfoResponse>> getUserInfo() {
        return ResourceConvertUtils.convertToResource(apiService.getUserInfo());
    }

    public LiveData<Resource<Object>> upHead(String url) {
        return ResourceConvertUtils.convertToResource(apiService.upHead(url));
    }

    public LiveData<Resource<Object>> setNickname(String name) {
        return ResourceConvertUtils.convertToResource(apiService.setNickname(name));
    }

    public LiveData<Resource<Object>> checkPhone(String code) {
        return ResourceConvertUtils.convertToResource(apiService.checkPhone(code));
    }

    public LiveData<Resource<Object>> setPhone(String phone, String code) {
        return ResourceConvertUtils.convertToResource(apiService.setPhone(phone, code));
    }

    public LiveData<Resource<Object>> setPwd(String oldPwd, String newPwd) {
        return ResourceConvertUtils.convertToResource(apiService.setPwd(oldPwd, newPwd));
    }

    public LiveData<Resource<Object>> feedBack(String text, String mobileMail) {
        return ResourceConvertUtils.convertToResource(apiService.feedBack(text, mobileMail));
    }

    public LiveData<Resource<Object>> logout() {
        return ResourceConvertUtils.convertToResource(apiService.logout());
    }

    public LiveData<Resource<GetSystemRecord>> getSystemMsg(int page, int pageSize) {
        return ResourceConvertUtils.convertToResource(apiService.getSystemMsg(page, pageSize));
    }

    public LiveData<Resource<GetSystemRecord>> getUserMsg(int page, int pageSize) {
        return ResourceConvertUtils.convertToResource(apiService.getUserMsg(page, pageSize));
    }

    public LiveData<Resource<GetPushEntity>> getSendSetting() {
        return ResourceConvertUtils.convertToResource(apiService.getSendSetting());
    }

    public LiveData<Resource<GetUserAddress>> getUserAddress() {
        return ResourceConvertUtils.convertToResource(apiService.getUserAddress());
    }
    public LiveData<Resource<Object>> setUserAddress(String address, String contactMan, String contactNumber) {
        return ResourceConvertUtils.convertToResource(apiService.setUserAddress(address, contactMan, contactNumber));
    }
    public LiveData<Resource<Object>> setRepair(String serialNumber, String cause, String address, String contactMan, String contactNumber) {
        return ResourceConvertUtils.convertToResource(apiService.setRepair(serialNumber, cause, address, contactMan, contactNumber));
    }

    public LiveData<Resource<Object>> registerSend(String clientBrand, String clientToken) {
        return ResourceConvertUtils.convertToResource(apiService.registerSend(clientBrand, clientToken));
    }

    public LiveData<Resource<Object>> setSendSetting(int stat, String startTime, String endTime) {
        return ResourceConvertUtils.convertToResource(apiService.setSendSetting(stat, startTime, endTime));
    }

    public LiveData<Resource<String>> publicUpImage(List<Bitmap> bitmaps) {
        MultipartBody.Builder body = new MultipartBody.Builder();
        for (Bitmap uri : bitmaps) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpeg"), ImageUtils.bitmapToBytes(uri));
            body.addFormDataPart("file", System.currentTimeMillis() + ".jpg", requestBody);
        }
        body.setType(MultipartBody.FORM);
        return ResourceConvertUtils.convertToResource(apiService.publicUpImage(Constants.IMAGE_PUSH_URL, body.build()));
    }
}
